﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using ReporterEngine.Model;

namespace ReporterControlLibrary
{
    public class InstanceTreeViewItem : TreeViewItem
    {
        public static readonly RoutedEvent CollapsingEvent =
       EventManager.RegisterRoutedEvent("Collapsing",
       RoutingStrategy.Bubble, typeof(RoutedEventHandler),
       typeof(InstanceTreeViewItem));

        public static readonly RoutedEvent ExpandingEvent =
            EventManager.RegisterRoutedEvent("Expanding",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler),
            typeof(InstanceTreeViewItem));

        public event RoutedEventHandler Collapsing
        {
            add { AddHandler(CollapsingEvent, value); }
            remove { RemoveHandler(CollapsingEvent, value); }
        }

        public event RoutedEventHandler Expanding
        {
            add { AddHandler(ExpandingEvent, value); }
            remove { RemoveHandler(ExpandingEvent, value); }
        }

        protected override void OnExpanded(RoutedEventArgs e)
        {
            OnExpanding(new RoutedEventArgs(ExpandingEvent, this));

            var item = e.OriginalSource as InstanceTreeViewItem;
            if (item != null)
            {
                var data = item.Header as InstanceCategory;
                if (data == null) throw new NotImplementedException();
                data.IsLoading = true;

                base.OnExpanded(e);

                data.IsLoading = false;
            }
        }
        //protected override void OnCollapsed(RoutedEventArgs e)
        //{
        //    OnCollapsing(new RoutedEventArgs(CollapsingEvent, this));
        //    base.OnCollapsed(e);
        //}
        //protected virtual void OnCollapsing(RoutedEventArgs e) { RaiseEvent(e); }
        protected virtual void OnExpanding(RoutedEventArgs e) { RaiseEvent(e); }
    }
}
